#pragma once

#include <angelscript.h>
#include <boostext/util/make_shared.hpp>
#include <engine.core/noncopyable.hpp>

#include "exports.hpp"
#include "pointer.hpp"
#include "Context.hpp"




namespace Engine
{
	namespace Script
	{
		template class ENGINE_SCRIPT_API pointer<asIScriptEngine>;




		class ENGINE_SCRIPT_API ContextPool
			: public noncopyable
		{
		public:

			typedef boost::shared_ptr<ContextPool>         ptr;
			typedef boost::shared_ptr<const ContextPool>   const_ptr;

		protected:

			ContextPool(pointer<asIScriptEngine> engine, Reflectable::weak_ptr reflectable);
			virtual void post_create(ptr _this);

		public:

			MAKE_SHARED2(ContextPool, pointer<asIScriptEngine>, Reflectable::weak_ptr);



			/**
			 * Returns a context that is ready to be used.
			 * The context should NOT be stored but used locally
			 * and then retrieved the next time it is required.
			 */
			Context::ptr context();

			/**
			 * Attaches a debugger to the contextpool.
			 */
			void attach(Debugger::ptr debugger);

		private:

			pointer<asIScriptEngine>    m_engine;
			Debugger::ptr               m_debugger;
			Reflectable::weak_ptr       m_reflectable;

			#pragma warning (push)
			#pragma warning (disable: 4251)
			std::vector<Context::ptr>   m_contexts;
			#pragma warning (pop)
		};
		///////////////////////////////////////////////////////////////////////////////////////////
	}
	///////////////////////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////////////
